import model.model as ut
from utils.common.db_connection import db, or_, and_
import view.work_order as view_wo
from enums.work_order_status_enum import WorkOrderStatusEnum
from enums.work_order_type_enum import WorkOrderTypeEnum
from sqlalchemy import func


def query_work_order_by_page(req_params: view_wo.Details, cur_user_id: int):
    """
    分页查询工单表
    :param req_params:
    :param cur_user_id:
    :return:
    """
    query = db.session.query(ut.WorkOrder)
    if cur_user_id != 21 or cur_user_id != 24:
        query = query.filter(ut.WorkOrder.ordered_time>1714492800000)
    if req_params.work_order_type:  # 过滤是否虚拟订单
        if WorkOrderTypeEnum.ALL.value == req_params.work_order_type:
            pass
        elif WorkOrderTypeEnum.NORMAL.value == req_params.work_order_type:
            query = query.filter_by(work_order_type=req_params.work_order_type)
        elif WorkOrderTypeEnum.VIRTUAL.value == req_params.work_order_type:
            query = query.filter_by(work_order_type=req_params.work_order_type)
    if req_params.customer_name is not None:
        query = query.filter(ut.WorkOrder.ww.like(f"%{req_params.customer_name}%"))
    if req_params.status >= 0:
        query = query.filter_by(status=req_params.status)
    if req_params.shop_id >= 0:
        query = query.filter_by(shop_id=req_params.shop_id)
    if req_params.order_id:
        query = query.filter_by(order_id=req_params.order_id)
    if req_params.oper_type:
        query = query.filter_by(oper_type=req_params.oper_type)
    if req_params.make_user_id:
        query = query.filter_by(make_user_id=req_params.make_user_id)
    if req_params.get_user_id:
        query = query.filter_by(get_user_id=req_params.get_user_id)
    if req_params.link:
        query = query.filter_by(link=req_params.link)
    total_count = query.count()
    order_by_field = ut.WorkOrder.ordered_time.desc()
    if req_params.status == 0:  # 如果是筛选未接单的单子，则按照创建时间升序排列
        order_by_field = ut.WorkOrder.ordered_time
    users = query.order_by(order_by_field).offset(
        req_params.page_num * req_params.page_size).limit(req_params.page_size).all()
    return total_count, users


def query_by_user_id(cur_user_id: int):
    """
    通过user_id查询制单和接单的数据
    :param cur_user_id: 用户id
    :return:
    """
    query = db.session.query(ut.WorkOrder).filter(or_(ut.WorkOrder.make_user_id == cur_user_id,
                                                      ut.WorkOrder.get_user_id == cur_user_id))
    total_count = query.count()
    return total_count


def query_work_order_by_id(id: int) -> ut.WorkOrder:
    """
    通过工单id查询工单详情
    :param id:  工单id
    :return:  工单详情
    """
    item = db.session.query(ut.WorkOrder).filter_by(id=id).first()
    return item


def query_by_link(link: str) -> ut.WorkOrder:
    """
    通过链接查询
    :return:  工单详情
    """
    return db.session.query(ut.WorkOrder).filter_by(link=link).order_by(ut.WorkOrder.ordered_time.desc()).first()


def query_work_order_by_order_id(order_id: int) -> ut.WorkOrder:
    """
    通过工单id查询工单详情
    :param order_id:  工单id
    :return:  工单详情
    """
    item = db.session.query(ut.WorkOrder).filter_by(order_id=order_id).first()
    return item


def modify_work_order_status_and_get_user_id(id: int, cur_status: int, cur_user_id: int) -> ut.WorkOrder:
    """
    修改工单状态
    :param id:
    :param cur_status:
    :param cur_user_id:
    :return:
    """
    item = ut.WorkOrder.query.get(id)
    if item:
        item.status = cur_status
        item.get_user_id = cur_user_id
        db.session.commit()
    return item


def modify_work_order_size(id: int, size: int) -> ut.WorkOrder:
    """
    修改工单份数
    :param id: 工单id
    :param size: 新的份数
    :return:
    """
    item = ut.WorkOrder.query.get(id)
    if item:
        item.total_size = size
        db.session.add(item)
        db.session.commit()
        db.session.close()
    return item


def modify_work_order_status(id: int, cur_status: int) -> ut.WorkOrder:
    """
    修改工单状态
    :param id:
    :param cur_status:
    :return:
    """
    item = ut.WorkOrder.query.get(id)
    if item:
        item.status = cur_status
        db.session.add(item)
        db.session.commit()
    return item


def modify_work_order(obj: ut.WorkOrder) -> ut.WorkOrder:
    db.session.add(obj)
    db.session.commit()
    return obj


def modify_work_order_link(id: int, link: str) -> ut.WorkOrder:
    """
    修改工单链接
    :param link:
    :param id:
    :return:
    """
    item = ut.WorkOrder.query.get(id)
    if item:
        item.link = link
        db.session.add(item)
        db.session.commit()
    return item


def modify_work_order_feedback(id: int, feedback: str) -> ut.WorkOrder:
    """
    修改工单链接
    :param feedback:
    :param id:
    :return:
    """
    item = ut.WorkOrder.query.get(id)
    if item:
        item.feedback = feedback
        db.session.commit()
    return item


def batch_insert(obj: [ut.WorkOrder]):
    # 批量提交
    db.session.add_all(obj)
    db.session.commit()


def commit_obj(obj: ut.WorkOrder):
    db.session.add(obj)
    db.session.commit()


def dashboard(req_params):
    """
    查询看板数据
    :param start_time:
    :param end_time:
    :param order_type:
    :return
    """
    start_time = req_params.get('start_time')
    end_time = req_params.get('end_time')
    shop_id = req_params.get('shop_id')
    query = db.session.query(ut.WorkOrder).filter(ut.WorkOrder.work_order_type != 'VIRTUAL')
    if start_time:
        query = query.filter(ut.WorkOrder.ordered_time >= int(start_time))
    if end_time:
        query = query.filter(ut.WorkOrder.ordered_time <= int(end_time))
    if shop_id:  # 存在筛选的门店，增加过滤条件
        query = query.filter(ut.WorkOrder.shop_id == int(shop_id))
    orders = query.all()
    users = db.session.query(ut.User).all()
    return orders, users
